package gudaziEx.service;

import com.sun.deploy.ui.ProgressDialog;
import org.apache.logging.log4j.core.util.SystemClock;

import javax.swing.text.View;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;

import static javafx.scene.input.KeyCode.R;

public class MainActivity {
    public static void main(String[] args) {
        try {
            //1. 得到连接对象
            String path = "https://download.apkpremier.com/?id=44919e8a85fe2066bd54e8997a88799ab73b05674f92f5b9e17a65098601581c96f3a3a02929380e195ef004479c533f74b3efbb2e92dff3d3dbceb5b1148ebf9b09ee65366f47914532f939fe690e493ba44f";
            URL url = new URL(path);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //2. 设置
            //connection.setRequestMethod("GET");

            connection.addRequestProperty("referer", "https://apkpremier.com");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(10000);
            //3. 连接
            connection.connect();
            //4. 请求并得到响应码200
            int responseCode = connection.getResponseCode();
            if(responseCode==200) {
                //5. 得到包含APK文件数据的InputStream
                InputStream is = connection.getInputStream();
                //6. 创建指向apkFile的FileOutputStream
                FileOutputStream fos = new FileOutputStream(new File("C:/Users/renzhehao/Desktop/Go.apk"));
                //7. 边读边写
                byte[] buffer = new byte[1024];
                int len = -1;
                while((len=is.read(buffer))!=-1) {
                    fos.write(buffer, 0, len);
                }

                fos.close();
                is.close();
            }
            //9. 下载完成, 关闭, 进入3)
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
